﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using CM.Resources.Web.Comm.Log;
using CM.Resources.Web.Controllers;
using CM.Resources.Web.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QL.Core.Caching;
using QL.Core.Log;

namespace CM.Resources.Web.Tests
{
    [TestClass]
    public class UnitTest1
    {
        static private ILogger log = RYLogger.GetLogger("HttpHandleController");
        static MemoryCaching cache = new MemoryCaching("file");
        [TestMethod]
        public void TestMethod1()
        {

            var result = Check("192.168.2.18");
            var result2 = Check("192.168.0.18");
            var result3 = Check("192.168.1.18");
            var result4 = Check("192.168.1.8");
            Assert.AreEqual(result, result2);
        }


        bool Check(string ip)
        {

            string limits = "192.168.1.8,192.168.0.0/24,0.0.0.0/0";
            if (!string.IsNullOrEmpty(limits))
            {
                return cache.GetOrAdd<bool>($"{limits}_{ip}", () =>
                {
                    if (ip.Equals("::1")) { return true; }
                    if (ip.Equals("127.0.0.1")) { return true; }
                    if (IPAddress.TryParse(ip, out IPAddress clientIp))
                    {

                        if (!string.IsNullOrEmpty(limits))
                        {
                            var checkList = cache.GetOrAdd<List<IPCheckInfo>>(limits, () =>
                            {
                                List<IPCheckInfo> list = new List<IPCheckInfo>();
                                var strArray = limits.Split(',');
                                foreach (var item in strArray)
                                {
                                    var ipAndMask = item.Split('/');
                                    if (ipAndMask.Length > 1)
                                    {
                                        if (int.TryParse(ipAndMask[1], out int subMaskBit))
                                        {
                                            IPAddress mask = null;
                                            try
                                            {
                                                mask = SubnetMask.CreateByNetBitLength(subMaskBit);
                                            }
                                            catch (Exception ex)
                                            {

                                                log.Error(ex, "");
                                            }
                                            if (mask != null)
                                            {
                                                if (IPAddress.TryParse(ipAndMask[0], out IPAddress address))
                                                {
                                                    list.Add(new IPCheckInfo()
                                                    {
                                                        Address = address,
                                                        IsIpAddress = false,
                                                        SubMask = mask
                                                    });
                                                }
                                                else
                                                {
                                                    log.Error("配置错误:{0}", item);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            log.Error("配置错误:{0}", item);
                                        }
                                    }
                                    else
                                    {
                                        if (IPAddress.TryParse(item, out IPAddress address))
                                        {
                                            list.Add(new IPCheckInfo()
                                            {
                                                Address = address,
                                                IsIpAddress = true
                                            });
                                        }
                                        else
                                        {
                                            log.Error("配置错误:{0}", item);
                                        }
                                    }
                                }
                                return list;
                            });

                            if (checkList.Count > 0)
                            {
                                var checkResult = checkList.AsParallel().Select(info =>
                                {
                                    if (info.IsIpAddress)
                                    {
                                        return info.Address.Equals(clientIp);
                                    }
                                    else
                                    {
                                        return clientIp.IsInSameSubnet(info.Address, info.SubMask);
                                    }

                                }).Contains(true);
                                return checkResult;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                });
            }
            else
            {

                return false;
            }

        }
    }
}
